#include "decodertest.h"



#include <newcode/detector/DetectorGC.h>
#include "testutils.h"
#include <QDebug>

using namespace zxing;
using namespace decoder;
using namespace detector;

const string DecoderTest::expectedStrings[] = {
    "test",
    "Qt Creator is an integrated development environment (IDE) that "
    "provides you with tools to design and develop applications with the "
    "Qt application framework. Qt is designed for developing applications "
    "and user interfaces once and deploying them across several desktop "
    "and mobile operating systems."
};

ArrayRef<unsigned char> DecoderTest:: selectWithMode(decoder::Decoder decoder,
    int x, int y, encoder::EncodeMode mode)
{
    ArrayRef<unsigned char> fromImage(16);
    decoder.getElementsFromImage(fromImage, x, y);
    return decoder.filterElement(fromImage, mode);
}

void DecoderTest::decodeBytesTest()
{
    ByteMatrix sample = ByteMatrix(4, 4);
    sample(0, 0)=0x32; sample(1, 0)=0xff; sample(2, 0)=0x39; sample(3, 0)=0xff;
    sample(0, 1)=0x30; sample(1, 1)=0xf8; sample(2, 1)=0x27; sample(3, 1)=0xf1;
    sample(0, 2)=0x37; sample(1, 2)=0x57; sample(2, 2)=0xed; sample(3, 2)=0x40;
    sample(0, 3)=0xed; sample(1, 3)=0x64; sample(2, 3)=0x80; sample(3, 3)=0x12;

    Decoder decoder;
    Ref<ByteMatrix> input = Ref<ByteMatrix>(&sample);
    input->retain();
    decoder.theBytes = input;
    ArrayRef<unsigned char> res = selectWithMode(decoder, 0, 0,
        encoder::ENCODE_MODE_2_LEVELS);
    QCOMPARE((int)res[0], 5);

    res =  selectWithMode(decoder, 2, 0, encoder::ENCODE_MODE_2_LEVELS);
    QCOMPARE((int)res[0], 13);
    res = selectWithMode(decoder, 0, 0, encoder::ENCODE_MODE_4_LEVELS);
    QCOMPARE((int)res[0], 32);
    QCOMPARE((int)res[1], 10);
    res = selectWithMode(decoder, 0, 0, encoder::ENCODE_MODE_3_LEVELS);
    QCOMPARE((int)res[1], 166);
    //input->release();
}

void DecoderTest::decodeFormatElementsTest()
{
    QFETCH(QString, fileName);
    QFETCH(int, dimension);
    QFETCH(int, size);
    QFETCH(int, mode);
    QString filename = TEST_DATA_BASE + fileName;
    QImage image(filename);

    if (!image.isNull())
    {
        Detector detector(qImageToByteMatrix(image));
        Ref<GrayscaleDetectorResult> result = detector.detect();
        Decoder decoder;
        decoder.decode(result->getBytes());
        QCOMPARE(decoder.dimension, dimension);
        QCOMPARE(decoder.formatSize, size);
        QCOMPARE(decoder.formatIsString, true);
        QCOMPARE((int)decoder.formatMode, mode);
    }
    else
    {
        QFAIL("Image file not found");
    }
}

void DecoderTest::decodeFormatElementsTest_data()
{
    QTest::addColumn<QString>("fileName");
    QTest::addColumn<int>("dimension");
    QTest::addColumn<int>("size");
    QTest::addColumn<int>("mode");
    QTest::newRow("test_2l.png") << "test_2l.png" << 12 << 4 << 0;
    QTest::newRow("test_4l.png") << "test_4l.png" << 12 << 4 << 1;
    QTest::newRow("test_8l.png") << "test_8l.png" << 12 << 4 << 2;
    QTest::newRow("test_3l.png") << "test_3l.png" << 12 << 4 << 3;
    QTest::newRow("long_2l.png") << "long_2l.png" << 60 << 296 << 0;
    QTest::newRow("long_4l.png") << "long_4l.png" << 48 << 296 << 1;
    QTest::newRow("long_8l.png") << "long_8l.png" << 40 << 296 << 2;
    QTest::newRow("long_3l.png") << "long_3l.png" << 52 << 296 << 3;
    QTest::newRow("transformed.png") << "transformed.png" << 12 << 4 << 3;

}

void DecoderTest::fullTest()
{
    QFETCH(QString, fileName);
    QFETCH(int, expectedTextIndex);
    QString filename = TEST_DATA_BASE + fileName;
    QImage image(filename);

    if (!image.isNull())
    {
        Detector detector(qImageToByteMatrix(image));
        Ref<GrayscaleDetectorResult> result = detector.detect();
        Decoder decoder;
        Ref<DecoderResult> decoderResult = decoder.decode(result->getBytes());
        QCOMPARE(decoderResult->getText()->getText(),
            expectedStrings[expectedTextIndex]);
    }
    else
    {
        QFAIL("Image file not found");
    }
}

void DecoderTest::fullTest_data()
{
    QTest::addColumn<QString>("fileName");
    QTest::addColumn<int>("expectedTextIndex");
    QTest::newRow("test_2l.png") << "test_2l.png" << 0;
    QTest::newRow("test_4l.png") << "test_4l.png" << 0;
    QTest::newRow("test_8l.png") << "test_8l.png" << 0;
    QTest::newRow("test_3l.png") << "test_3l.png" << 0;
    QTest::newRow("long_2l.png") << "long_2l.png" << 1;
    QTest::newRow("long_4l.png") << "long_4l.png" << 1;
    QTest::newRow("long_8l.png") << "long_8l.png" << 1;
    QTest::newRow("long_3l.png") << "long_3l.png" << 1;
    QTest::newRow("transformed.png") << "transformed.png" << 0;
}
